package de.hb.bv1.u04;

import ij.ImagePlus;
import ij.plugin.filter.PlugInFilter;
import ij.process.ImageProcessor;

/**
 * Example of how to separate logic and gui
 * All Plugins have to implement the PlugInFilter
 * 
 * @author gruppe13
 * @version 2011-11-20
 *
 */
public class Gruppe13_U04_Plugins implements PlugInFilter {

	  /**
	   * Used to identify which filter to run
	   * @author gruppe13
	   * @see plugins.config
	   */
	  private enum PluginFunctions {
	    NONE, MEDIAN, ADDSHADING, MULSHADING
	  };
	  private PluginFunctions func = PluginFunctions.NONE;

	  /**
	   * Method which is called when the filter is loaded
	   * 
	   * @param arg the argument given in plugins.config
	   * @param imp the currently active image
	   */
	  @Override
	  public int setup(String arg, ImagePlus imp) {
	    for (PluginFunctions inv : PluginFunctions.values()) {
	      if (inv.name().equals(arg)) {
	        func = inv;
	        break;
	      }
	    }
	    //we can handle 8-bit grayscale images
	    return DOES_8G;
	  }

	  /** 
	   * Called once to process the image
	   */
	  @Override
	  public void run(ImageProcessor ip) {
	    long startTime = System.nanoTime();
		    switch (func) {
		    case MEDIAN:
		      // (!)
		      Gruppe13_U04_Logic.median(ip);
		      break;
		    case ADDSHADING:
			      // (!)
			  Gruppe13_U04_Logic.addShading(ip);  
		      break;
		    case MULSHADING:
			      // (!)
			  Gruppe13_U04_Logic.mulShading(ip);  
		      break;
		    
		    }
	    System.out.print(String.format("%s processing time: %.4fs\n", func.name(),
	        (System.nanoTime() - startTime) * 1e-9));
	  }

	}